//申请单-超期超额客户发货申请
public without sharing class OverdueExcessHandler {
    	

   	public  static void getLastMonthMoney(List<OverdueExcess__c> olist ){



         
   		Map<Id,Decimal> oMap = new Map<Id,Decimal>();
   		Set<String> companySet = new Set<String>();
         Set<Id> oexset = new Set<Id>();
   		for (OverdueExcess__c o  : olist) {
   			//o.Company__c = '迈克生物股份有限公司'
   			if(o.Company__c == null ) {
   				o.addError('客户所属公司不能为空!'); 
   				return;
   			}
   			if(o.CustomerName__c != null ) 
               oMap.put(o.CustomerName__c, null);
   			//companyMap.put(o.CustomerName__c,o.Company__c);
   			companySet.add(o.Company__c);
            if(o.Id != null)oexset.add(o.Id);


   		}
          //<客户Id,超期申请>
         Map<Id,OverdueExcess__c> oaMap = new Map<Id,OverdueExcess__c>();
         for(OverdueExcess__c att :[select Id, PlannedAmountLastMonth__c,PlannedAmount__c,CustomerName__c , LastModifiedDate from OverdueExcess__c where CustomerName__c in:oMap.keySet()]){
            if(oexset.contains(att.Id))continue;
            OverdueExcess__c oldO = oaMap.get(att.CustomerName__c);
            oaMap.put(att.CustomerName__c,oldO == null || (att.LastModifiedDate.getTime() - oldO.LastModifiedDate.getTime()) >=0  ? att : oldO );
            

         }
      
      
   		
         Map<Id,String > accMap = new Map<Id,String>();

   		for (Account acc  :[select Id, AccountNumber from Account where Id in: oMap.keySet() and AccountNumber != null  ] ) {
   			if(acc.AccountNumber != null )
               accMap.put(acc.Id, acc.AccountNumber);
   		}

   		//找回款
   		Date d1 = System.today().addMonths(-1).toStartOfMonth();//上月1号
   		Date d2 = System.today().toStartOfMonth().addDays(-1);//上月最后一天
         Date d3 = System.today().toStartOfMonth();//本月1号
         Date d4 = System.today();//返回当天
   		// SalesReceiptDetail__c sa 
   		// sa.corp_name__c 
   		// sa.corp_name__c
         //计算上月回款总额
   		Map<String,Decimal> accMoneyMap = new Map<String,Decimal>();
   		for (AggregateResult sdp  :[select sum(bbje__c) money,custcode__c,corp_name__c from SalesReceiptDetail__c 
   			where custcode__c in: accMap.values() and doc_date__c >=:d1 and  doc_date__c<=:d2 and corp_name__c in:companySet group by custcode__c,corp_name__c ] ) {
   			
   			accMoneyMap.put(sdp.get('custcode__c')+''+sdp.get('corp_name__c'), Decimal.valueOf(sdp.get('money')+''));


   		}
         //计算本月回款总额
         Map<String,Decimal> accMoneyMapNew = new Map<String,Decimal>();

         for (AggregateResult sdp  :[select sum(bbje__c) money,custcode__c,corp_name__c from SalesReceiptDetail__c 
            where custcode__c in: accMap.values() and doc_date__c >=:d3 and  doc_date__c<=:d4 and corp_name__c in:companySet group by custcode__c,corp_name__c ] ) {
            
            accMoneyMapNew.put(sdp.get('custcode__c')+''+sdp.get('corp_name__c'), Decimal.valueOf(sdp.get('money')+''));


         }
   		//System.debug(accMoneyMap);
   		//System.debug(accMap);

   		for (OverdueExcess__c o  : olist ) {
   			
   			if(o.CustomerName__c != null  && accMoneyMap.containsKey(accMap.get(o.CustomerName__c) +o.Company__c) ){
               OverdueExcess__c last = oaMap.get(o.CustomerName__c);
               if(last != null) o.PlannedAmountLastMonth__c = last.PlannedAmount__c;
   				o.LastMonth__c = accMoneyMap.get(accMap.get(o.CustomerName__c) +o.Company__c ) == null ? null : accMoneyMap.get(accMap.get(o.CustomerName__c)+o.Company__c)/10000; 
   			}else{
   				o.LastMonth__c  = null;
   			}

            if(o.CustomerName__c != null  && accMoneyMapNew.containsKey(accMap.get(o.CustomerName__c) +o.Company__c) ){
               OverdueExcess__c lastNew = oaMap.get(o.CustomerName__c);
               o.ThisMonth__c = accMoneyMapNew.get(accMap.get(o.CustomerName__c) +o.Company__c ) == null ? null : accMoneyMapNew.get(accMap.get(o.CustomerName__c)+o.Company__c); 
            }else{
               o.ThisMonth__c  = null;
            }


   		}


   	}

}